package com.Bigbluebook.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.Bigbluebook.Activity.PostDetailsActivity;
import com.Bigbluebook.R;
import com.Bigbluebook.entity.PostsInfo;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


/**
 *创建文章适配器
 */
public class PostsInfoListAdapter extends RecyclerView.Adapter<PostsInfoListAdapter.MyHolder> {
    private List<PostsInfo> postsInfoList =new ArrayList<>();

    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(PostsInfo post);
    }

    private OnItemClickListener onItemClickListener;

    // 设置点击事件监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setPostsInfoList(List<PostsInfo> postsInfos){
        this.postsInfoList =postsInfos;
         notifyDataSetChanged();

    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts_item,parent,false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        //绑定数据
        PostsInfo postsInfo = postsInfoList.get(position);

        holder.author.setText(postsInfo.getUser().getUsername());
        holder.content.setText(postsInfo.getContent());

        //日期格式化
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(postsInfo.getCreatedAt());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String formattedDate = zonedDateTime.format(formatter);
        holder.create_time.setText(formattedDate);

        // 添加点击事件
        holder.itemView.setOnClickListener(v -> {
            // 使用 Intent 传递 postId 到 PostDetailsActivity
            Intent intent = new Intent(holder.itemView.getContext(), PostDetailsActivity.class);
            intent.putExtra("postId", postsInfo.getId());  // 假设 PostsInfo 类有 getId() 方法
            intent.putExtra("userid",postsInfo.getUser().getId());
            Log.e("点击博文","用户数据"+ postsInfo.getUser().getId());
            holder.itemView.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return postsInfoList.size();
    }

    static class  MyHolder extends RecyclerView.ViewHolder{
        TextView author;
        TextView content;
        TextView create_time;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            author =itemView.findViewById(R.id.author);
            content =itemView.findViewById(R.id.content);
            create_time =itemView.findViewById(R.id.create_time);
        }
    }
}
